// 启动一个web server
package main

import (
	"encoding/json"
	"log"
	"net/http"
)

func main() {
	// 访问：http://127.0.0.1:8888/hello
	http.HandleFunc("/hello", myHelloWorld)
	http.HandleFunc("/hello2", myHelloWorld2)
	err := http.ListenAndServe("0.0.0.0:8888", nil)
	if err != nil {
		log.Fatal(err)
	}
}

func myHelloWorld2(writer http.ResponseWriter, request *http.Request) {
	dataMap := map[string]string{}
	dataMap["name"] = "pss"
	dataMap["age"] = "123"
	bytes, err := json.Marshal(dataMap)
	if err != nil {
		log.Fatal(err)
	}
	_, err = writer.Write(bytes)
	if err != nil {
		log.Fatal(err)
	}
}

// 处理一个uRL
func myHelloWorld(writer http.ResponseWriter, request *http.Request) {
	responseBytes := []byte("hello gogogogo")
	_, err := writer.Write(responseBytes)
	if err != nil {
		log.Fatal(err)
	}
}
